<?php
/**
 * 后台 Button Widget
 * User: ZFC
 * Date: 2015/7/6
 * Time: 11:35
 */

namespace common\widgets;


use yii\helpers\Html;

class Button extends Widget {

    //<button>或者<a>
    public $tagName = 'button';

    public $label = '按钮';

    public $encodeLabel = true;

    //图标选项
    public $icon;

    //Modal选项 填入目标的ID
    public $modal;


    /**
     * Initializes the widget.
     * If you override this method, make sure you call the parent implementation first.
     */
    public function init()
    {
        parent::init();
        $this->clientOptions = false;
        Html::addCssClass($this->options, 'btn');
    }

    /**
     * Renders the widget.
     */
    public function run()
    {

        $this->registerPlugin('button');
        if($this->modal !== null){
            $this->options['data-toggle'] = 'modal';
            $this->options['data-target'] = $this->modal;
        }
        $button = Html::beginTag($this->tagName, $this->options);
        if($this->icon != ""){
            $button .= Html::tag('i', '', ['class'=>'fa fa-' . $this->icon]) . '&nbsp;';
        }
        $button .= $this->encodeLabel ? Html::encode($this->label) : $this->label;
        $button .= Html::endTag($this->tagName);
        return $button;
//        return Html::tag($this->tagName, $this->encodeLabel ? Html::encode($this->label) : $this->label, $this->options);
    }
}